package com.sczmgk.hotline;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;

import com.sczmgk.hotline.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline.httpUtils.MyHttp;
import com.sczmgk.hotline.utils.MyLog;
import com.sczmgk.hotline.utils.Tools;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 抓取异常的类
 * @author bo
 *
 */
@SuppressLint("HandlerLeak")
public class MyCrashHandler implements UncaughtExceptionHandler {

	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			MyLog.d(TAG,"开始自杀");
			android.os.Process.killProcess(android.os.Process.myPid());
		};
	};
	private static final String TAG = "MyCrashHandler";
	private Context context;
	private static MyCrashHandler instance;
	public static synchronized MyCrashHandler getInstance(){
		if(instance == null){
			instance = new MyCrashHandler();
		}
		return instance;
	}
	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		StringBuilder sb = new StringBuilder();
		PackageManager pm = context.getPackageManager();
		try {
			MyLog.d(TAG, "取手机硬件信息及应用版本号 ");
			sb.append("\n");
			String dataStr = Tools.getData();
			sb.append(dataStr+"--------------->");
			sb.append("\n");
			PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
			//应用版本号
			sb.append("乐政版本号："+packageInfo.versionName);
			sb.append("\n");
			//获取手机的硬件信息
			Field[] fields = Build.class.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				//暴力反射
				fields[i].setAccessible(true);
				String name = fields[i].getName();
				sb.append(name + " = ");
				String value = fields[i].get(null).toString();
				sb.append(value);
				sb.append("\n");
			}
			//获取程序错误的堆栈信息
			StringWriter wr = new StringWriter();
			PrintWriter printWriter = new PrintWriter(wr);
			ex.printStackTrace(printWriter);
			String result = wr.toString();
			sb.append(result);
			//上传异常到网络
			Map<String,Object> exceptionMaps = new HashMap<String,Object>();
			exceptionMaps.put("exceptiontype", 1);
			exceptionMaps.put("apptype", 0); //应用类型 0=乐政 1=业务
			exceptionMaps.put("exceptioncontent", sb.toString());
			//TODO 异常url
			MyHttp.request(context, "", exceptionMaps, MyHttp.POST, false, null, new SimplePaserJsonCallback() {
				@Override
				public void onVerifyIsNull() {

				}

				@Override
				public void onVerifySuccess(String dataRaw) {

				}

				@Override
				public void onServerUnKnownError() {

				}

				@Override
				public void onErrorCodeError(String msg) {

				}
			});

			
		} catch (Exception e) {
			MyLog.e(TAG, "取手机硬件信息错误！ ");
			e.printStackTrace();

			//获取程序错误的堆栈信息
			StringWriter wr = new StringWriter();
			PrintWriter printWriter = new PrintWriter(wr);
			ex.printStackTrace(printWriter);
			String result = wr.toString();
			sb.append(result);
			//上传异常到网络
//			LogFile.writeLog(sb.toString(),"exception.txt");
			Map<String,Object> exceptionMaps = new HashMap<String,Object>();
			exceptionMaps.put("exceptiontype", 1);
			exceptionMaps.put("apptype", 0); //应用类型 0=乐政 1=业务
			exceptionMaps.put("exceptioncontent", sb.toString());
			//TODO 异常url
			MyHttp.request(context, "", exceptionMaps, MyHttp.POST, false, null, new SimplePaserJsonCallback() {
				@Override
				public void onVerifyIsNull() {

				}

				@Override
				public void onVerifySuccess(String dataRaw) {

				}

				@Override
				public void onServerUnKnownError() {

				}

				@Override
				public void onErrorCodeError(String msg) {

				}
			});
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					android.os.Process.killProcess(android.os.Process.myPid());
				}
			}, 1000);
		}
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				android.os.Process.killProcess(android.os.Process.myPid());
				
			}
		}, 1000);
	}
	public void setContext(Context context) {
		this.context = context;
	}

}
